package mulMan.gui.view;

import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import mulMan.domain.AudioFileModel;

public class AudioFilesView extends JPanel implements Observer{

	private static final long serialVersionUID = 9219019438747502674L;
	
	private List<AudioFileModel> audioFiles;
	private JTable tableOfAudioFiles;
	private JPopupMenu popupMenu;

	public JTable getTableOfAudioFiles() {
		return tableOfAudioFiles;
	}
	public void setTableOfAudioFiles(JTable tableOfAudioFiles) {
		this.tableOfAudioFiles = tableOfAudioFiles;
	}
	public JPopupMenu getPopupMenu() {
		return popupMenu;
	}
	public void setPopupMenu(JPopupMenu popupMenu) {
		this.popupMenu = popupMenu;
	}
	
	
	public AudioFilesView(List<AudioFileModel> audioFiles) {
		this.audioFiles = audioFiles;
		tableOfAudioFiles = new JTable();

		for (AudioFileModel iterator : audioFiles) {
			iterator.addObserver(this);
		}
		
		initView();
	}

	private void initView() {

	    this.setLayout(null);
	    setTable();
	    
	    setPopup();
		
		JScrollPane listScroller = new JScrollPane(tableOfAudioFiles);
	    listScroller.setPreferredSize(new Dimension(250, 80));
	    listScroller.setBounds(5, 5, 585, 340);
	    this.add(listScroller);
	}
	
	private void setPopup() {
		JMenuItem menuItemDelete = new JMenuItem("Remove Audio File");
		popupMenu = new JPopupMenu();
		popupMenu.add(menuItemDelete);
	}
	private void setTable() {
		String[] tableHeader = {"Title", "Artist", "Album", "Length"};
		String[][] data = new String[audioFiles.size()][tableHeader.length];

		int i = 0;
		for (AudioFileModel iterator : audioFiles) {
			data[i][0] = iterator.getAudioFile().getTitle();
			data[i][1] = iterator.getAudioFile().getArtist();
			data[i][2] = iterator.getAudioFile().getAlbum();
			data[i][3] = String.valueOf(iterator.getAudioFile().getLength());
			
			i++;
		}
		
		DefaultTableModel model = new DefaultTableModel(data, tableHeader) {
			private static final long serialVersionUID = -8595519407607302794L;

			public boolean isCellEditable(int rowIndex, int mColIndex) {
		        return false;
			}
		};
	    
		tableOfAudioFiles.setModel(model);
		tableOfAudioFiles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		
	    
		for (AudioFileModel iterator : audioFiles) {
			iterator.addObserver(this);
		}
	}

	public void update(Observable o, Object arg) {
		resetList();
	}
	
	public void resetList() {
		
		for (AudioFileModel iterator : audioFiles) {
			iterator.addObserver(this);
		}
		
		setTable();
		
		this.updateUI();
	}
	
	

	public void addTableAudioFilesListener(MouseListener tafl) {
		tableOfAudioFiles.addMouseListener(tafl);
    }
}
